package com.mlnx.device.pojo.dto;


import com.mlnx.data.entity.BpInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by amanda.shan on 2019/7/30.
 */
@Slf4j
public class BpCache {

    private Map<Channel, List<BpInfo>> bpChannelCache = new ConcurrentHashMap<>();

    static BpCache BP_CACHE = new BpCache();

    public static BpCache getInstance() {
        return BP_CACHE;
    }


    public void addBpinfo(Channel channel, BpInfo bpMessage) {

        List<BpInfo> bpMessages = bpChannelCache.get(channel);
        if (bpMessages == null) {
            bpMessages = new ArrayList<>();
            bpChannelCache.put(channel, bpMessages);
        }
        bpMessages.add(bpMessage);
        checkChannel();
    }

    public List<BpInfo> getBpList(Channel channel) {

        return bpChannelCache.get(channel);
    }

    private void checkChannel() {
        for (Channel channel : bpChannelCache.keySet()) {
            if (!channel.isActive()) {
                bpChannelCache.remove(channel);

            }
        }
    }


}
